package com.njitzx.service.impl;

import com.njitzx.en.OrderEnum;
import com.njitzx.en.PayMethodEnum;
import com.njitzx.entity.po.Goods;
import com.njitzx.entity.po.Order;
import com.njitzx.entity.po.OrderGoods;
import com.njitzx.entity.vo.*;
import com.njitzx.mapper.*;
import com.njitzx.service.ReportService;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;


import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final MemberMapper memberMapper;

    private final GoodsMapper goodsMapper;

    private final OrderMapper orderMapper;
    private final OrderGoodsMapper orderGoodsMapper;
    private final CategoryMapper categoryMapper;

    @Override
    public LiveReportVO getLiveReport() {
        int memberCount = memberMapper.getCount();
        int goodsCount = goodsMapper.getCount();
        Double salesAmount = orderMapper.getSalesAmount();
        Integer orderCount = orderMapper.getCount();
        return new LiveReportVO(memberCount, goodsCount, orderCount, salesAmount);
    }

    @Override
    public TurnoverVO getTurnoverReport() {
        List<Order> select = orderMapper.select(new Order());
//        Map<String, Double> turnoverMap = select.stream().collect(Collectors.groupingBy(
//                order -> order.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")),
//                Collectors.summingDouble(Order::getOrderPrice)
//        ));

        Map<String, Double> turnoverMap = select.stream().collect(Collectors.groupingBy(
                order -> order.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM--dd")),
                Collectors.summingDouble(Order::getOrderPrice)
        ));
        String dateList = String.join(",", turnoverMap.keySet());
        String turnoverList = turnoverMap.values().stream()
                .map(amount -> String.format("%.2f", amount))
                .collect(Collectors.joining(","));

        // 创建并返回 TurnoverVO 对象
        TurnoverVO turnoverVO = new TurnoverVO();
        turnoverVO.setDateList(dateList);
        turnoverVO.setTurnoverList(turnoverList);
        return turnoverVO;
    }

    @Override
    public OrderStatusVO getOrderStatus() {
        List<Order> select = orderMapper.select(new Order());

        Map<String, Long> collect = select.stream().collect(Collectors.groupingBy(order -> {
            return OrderEnum.values()[order.getStatus()].getDesc();
        }, Collectors.counting()));
        System.out.println(collect);
        String statusList = String.join(",", collect.keySet());
        String countList = collect.values().stream().map(item -> item.toString()).collect(Collectors.joining(","));
        return new OrderStatusVO(countList, statusList);
    }

    @Override
    public PayMethodVO getPayMethod() {
        List<Order> select = orderMapper.select(new Order()).stream().filter(item -> !item.getPayMethod().equals("0")).collect(Collectors.toList());
        System.out.println(select);

        Map<String, Long> collect = select.stream().collect(Collectors.groupingBy(item -> {
            return PayMethodEnum.values()[Integer.parseInt(item.getPayMethod())].getMethod();
        }, Collectors.counting()));

        String methodList = String.join(",", collect.keySet());

        String countList = collect.values().stream().map(item -> item.toString()).collect(Collectors.joining(","));

        return new PayMethodVO(countList, methodList);

    }

    @Override
    public Top10VO top10() {

        List<OrderGoods> select = orderGoodsMapper.select(new OrderGoods());

        Map<Integer, Long> collect = select.stream().collect(Collectors.groupingBy(item -> {
            return item.getGoodsId();
        }, Collectors.counting()));
        //拿到商品的id
        System.out.println("collect"+collect);
        Set<Integer> integers = collect.keySet();
        Map<String, Long> res = new HashMap<>();

        for (Integer goodsid : integers) {
            Goods goods = goodsMapper.selectById(goodsid); //拿到商品
            Integer firstId = goods.getCatOneId();
            String name = categoryMapper.selectById(firstId).getCatName();
            Long count = collect.get(goodsid);
            //放入map中
            res.put(name, res.getOrDefault(name,0L)+count);
        }

        String nameList = String.join(",", res.keySet());
        String numberList = res.values().stream().map(item -> item.toString()).collect(Collectors.joining(","));
        return new Top10VO(nameList,numberList);
    }
}
